import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';

/// 带数据底部弹窗
class BaseRequestFormSelectDown extends StatefulWidget {
  final BaseHttpRequestModel request;
  final RequestSaveData requestSaveData;

  BaseRequestFormSelectDown({Key key, @required this.request, this.requestSaveData}) : super(key: key);
  @override
  _BaseRequestFormSelectDownState createState() => _BaseRequestFormSelectDownState();
}

class _BaseRequestFormSelectDownState extends State<BaseRequestFormSelectDown> {
  /// 入参
  BaseHttpRequestModel get _request => widget.request;
  BaseRequestFormProvider _baseRequestFormProvider;
  List<BaseBottomSheetSelectorModel> _items = [];
  BaseBottomSheetController _baseBottomSheetController;
  @override
  void initState() {
    /// 检测是否存在值
    if (_request.selectData != null && _request.selectData is List<BaseKeyValueModel>) {
      if (_request.selectData.length > 0) {
        /// map 以  key , value 的形式插入
        _items = BaseBottomSheetSelectorModel.getListBaseSelectDownItem(_request.selectData.map((e) => e.toJson()).toList());
      }
    }
    _baseBottomSheetController = BaseBottomSheetController(items: _items);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _baseRequestFormProvider = BaseRequestFormProvider.of(context);

    /// 获取对应值
    try {
      _baseBottomSheetController.selectedItem = Map<String, dynamic>.from(_baseRequestFormProvider.getDataSource(_request));
    } catch (error) {
      print(error);
    }
    _baseBottomSheetController.hit = widget.request.notes;

    return BaseRequestFormContainer(
      suffixIcon: BaseDropDownIcon(),
      showSuffixIcon: !_baseRequestFormProvider.isReadonly(widget.request),
      request: widget.request,
      onPress: _baseRequestFormProvider.isReadonly(_request) == true ? null : () => {_baseBottomSheetController.showBottomSheet()},
      child: BaseBottomSheetSelector(
        isIcon: false,
        controller: _baseBottomSheetController,
        disable: true,
        onChange: (value) {
          _baseRequestFormProvider.setDataSource(_request, value);
          if (widget.requestSaveData != null) {
            widget.requestSaveData(widget.request, _baseRequestFormProvider.getMapDataSource(widget.request));
          }
        },
      ),
    );
  }
}
